package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory.kaifang.addChuFang

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityCustomChuFangBinding
import com.lizardmind.haofudoctor.databinding.ItemCustomChuFangBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.CustomChuFangMedicalBean
import com.lizardmind.haofudoctor.other.AddMedicineEvent
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_custom_chu_fang.*
import kotlinx.android.synthetic.main.head_custom_chu_fang.*
import kotlinx.android.synthetic.main.include_title_text.*
import kotlinx.android.synthetic.main.item_custom_chu_fang.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.sdk25.coroutines.onClick

//自定义处方activity
class CustomChuFangActivity : BaseActivity() {

    private val list= arrayListOf<CustomChuFangMedicalBean>()

    private val adapter by lazy { BaseAdapter<CustomChuFangMedicalBean,ItemCustomChuFangBinding>(R.layout.item_custom_chu_fang,list) }
    private lateinit var headView: View
    private val binding by lazy { getDataBinding<ActivityCustomChuFangBinding>(R.layout.activity_custom_chu_fang) }
    override fun onCreatRootView(): View? {
        headView=LayoutInflater.from(this).inflate(R.layout.head_custom_chu_fang,null)
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        setEvent(true)
        titleText.text="自定义处方"
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        imgRight.setImageResource(R.mipmap.kf_sure)
        imgRight.onClick {
            if (etChufangName.length()==0||TextUtils.isEmpty(etChufangName.text.toString())){
                toasts("请输入处方名")
                return@onClick
            }
            if (list.size==0){
                toasts("请添加药材")
                return@onClick
            }
            for (i in 0 until list.size){
                if (list[i].medicalWeight=="0"||list[i].medicalWeight=="0.0"){
                    toasts("请输入药材重量")
                    return@onClick
                }
            }

            postChuFang()

        }
        tvAddMedical.onClick { startActivity(Intent(this@CustomChuFangActivity,ChooseMedicineActivity::class.java)) }

        adapter.headerView=headView
        rvCustomChuFang.apply {
            layoutManager=GridLayoutManager(this@CustomChuFangActivity,2)
            adapter=this@CustomChuFangActivity.adapter
        }

        adapter.onBind { itemBingding, position, data ->
            itemBingding.apply {
                tvMedicalName.onClick {
                    list.removeAt(position)
                    adapter.notifyDataSetChanged()
                }

                itemBingding.etWeight.addTextChangedListener(object : TextWatcher{
                    override fun afterTextChanged(p0: Editable?) {
                        list[position].medicalWeight=etWeight.text.toString()
                    }
                    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                    }
                    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                    }
                })

            }
        }

    }

    @Subscribe
    fun getDiyMedicineData(event:AddMedicineEvent){
        (0 until event.list.size).mapTo(list) { CustomChuFangMedicalBean(event.list[it],"0") }
        adapter.notifyDataSetChanged()
    }

    private fun postChuFang(){

        var chufang=""
        for (i in 0 until list.size){
            //该处逗号为中文符  将处方拼装成eg： 白果 12g，莲子 1g，陈皮 50g
            chufang+=list[i].medicalName+" "+list[i].medicalWeight+if (i!=list.size-1) "g，" else "g"
        }

        observe {
            postApi<Request<String>>(hashMapOf(
                    "action" to "add_cyyao",
                    "token" to DoctorInfo.token,
                    "type" to "2",
                    "title" to etChufangName.text.toString(),
                    "content" to chufang
            ), Consumer {

            }, Action {
                finish()
            },context = this)
        }

    }

}
